
from flask import current_app, jsonify,abort,g,render_template,session
from flask import request

from info import db
from info.models import News, User, Comment, CommentLike
from info.utils.commons import user_login_data
from info.utils.response_code import RET
from . import news_blue





@news_blue.route('/comment_like',methods=['post'])
@user_login_data
def comment_like():
    #1判断用户是否有登陆
    if not g.user:
        return jsonify(errno=RET.NODATA,errmsg="用户未登陆")
    #2获取参数
    comment_id = request.json.get("comment_id")
    action = request.json.get("action")
    #3参数校验
    if not all([comment_id,action]):
        return jsonify(errno=RET.PARAMERR,errmsg="参数不全")
    #4操作类型进行校验
    if not action in ("add","remove"):
        return jsonify(errno=RET.DBERR,errmsg="操作类型有误")
    #5通过评论编号查询评论对象.并判断是否存在
    try:
        comment = Comment.query.get(comment_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取评论失败")
    if not comment:return jsonify(errno=RET.NODATA,errmsg="评论不不存在")
    #6根据操作类型点赞取消点赞
    try:
        if action == "add":
            comment_like = CommentLike.query.filter(CommentLike.user_id == g.user.id,CommentLike.comment_id == comment_id).first()
            if not comment_like:
                # 创建点赞对象
                comment_like = CommentLike()
                comment_like.user_id = g.user.id
                comment_like.comment_id = comment_id
                # 添加到数据库中
                db.session.add(comment_like)

                comment.like_count +=1
                db.session.commit()

        else:
            comment_like = CommentLike.query.filter(CommentLike.user_id == g.user.id,
                                                    CommentLike.comment_id == comment_id).first()
            if comment_like:
                db.session.delete(comment_like)

                if comment.like_count > 0:
                    comment.like_count -= 1
                    db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="操作失败")

    #7返回响应
    return jsonify(errno=RET.OK,errmsg="操作成功")




"""
请求路径: /news/news_comment
请求方式: POST
请求参数:news_id,comment,parent_id
返回值: errno,errmsg,评论字典
"""
# 评论接口
@news_blue.route('/news_comment',methods=['POST'])
@user_login_data
def news_comment():

    #1判断用户是都登陆
    if not g.user:
        return jsonify(errno=RET.PARAMERR,errmsg="用户未登录")
    #2获取请求参数
    news_id = request.json.get("news_id")
    content = request.json.get("comment")
    parent_id = request.json.get("parent_id")
    #3校验参数.为空判断
    if not all([news_id,content]):
        return jsonify(errno=RET.PARAMERR,errmsg="参数不全")
    #4根据新闻标号取出新闻对象,判断新闻是否存在
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")
    if not news:return jsonify(errno=RET.NODATA,errmsg="新闻不存在")
    #5创建评论对象,设置属性
    comment= Comment()
    comment.user_id = g.user.id
    comment.news_id = news_id
    comment.content = content
    if parent_id:
        comment.parent_id = parent_id
    #6保存评论对象到数据库中
    try:
        db.session.add(comment)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.OK,errmsg="评论成功")

    #7返回响应,携带评论的数据
    return jsonify(errno=RET.OK,errmsg="评论成功",data=comment.to_dict())

#点击接口
@news_blue.route('/news_collext',methods=['POST'])
def news_collect():
    #1判断用户是否登陆了
    if not g.user:
        return jsonify(errno=RET.NODATA,errmsg="用户未登陆")
    #2获取参数
    news_id = request.json.get("news_id")
    action = request.json.get("action")
    #3参数效验,为空效验
    if not all([news_id,action]):
        return jsonify(errno=RET.PARAMERR,errmsg="参数不全")
    #4操作类型效验
    if not action in ["collect","cancel_collect"]:
        return jsonify(errno=RET.DATAERR,errmsg="操作类型有误")
    #5根据新闻的编号取出新闻对象
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")
    #6判断新闻对象是否存在
    if not news:
        return jsonify(errno=RET.NODATA,errmsg="新闻不存在")
    #7根据操作类型,进行收藏.取消收藏操作
    if action == "collect":
        if not news in g.user.collection_news:
            g.user.collection_news.append(news)
    else:
        if news in g.user.collection_news:
            g.user.collection_news.remove(news)
    #8返回响应
    return jsonify(errno=RET.OK,errmsg="操作成功")



# 排行接口
@news_blue.route('/<int:news_id>')
@user_login_data
def news_detail(news_id):

    # 2根据新闻编号,查询新闻对象
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")

    # 如果新闻对象不存在直接跑出异常
    if not news:
        abort(404)

    # 3获取前6条热门新闻
    try:
        click_news = News.query.order_by(News.clicks.desc()).limit(6).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取失败")
    # 4将热门新闻的对象列表转成字典列表
    click_news_list = []
    for item_news in click_news:
        click_news_list.append(item_news.to_dict())

    # v5判断用户是否收藏过该新闻
    is_collected = False
    if g.user:
        if news in g.user.collection_news:
            is_collected = True


    # 6查询数据库中,该新闻的所有评论内容
    try:
        comments = Comment.query.filter(Comment.news_id == news_id).order_by(Comment.create_time.desc()).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取评论失败")
    # 该用户点赞过的评论编号
    try:
        commentlikes = []
        if g.user:
            commentlikes = CommentLike.query.filter(CommentLike.user_id == g.user.id).all()

    # 获取用户所有点赞过的评论编号
        mylike_comment_ids = []
        for commentLike in commentlikes:
            mylike_comment_ids.append(commentLike.comment_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取点赞失败")

    # 7将评论的对象列表,转成字典列表
    comments_list = []
    for comment in comments:
        comm_dict = comment.to_dict()
        comm_dict["is_like"] = False

        # 判断用户是否有对评论点过赞
        if g.user and comment.id in mylike_comment_ids:
            comm_dict["is_like"] = True
        comments_list.append(comm_dict)


    is_followed = False
    if g.user and news.user:
        if g.user in news.user.followers:
            is_followed = True

    # 2携带数据,渲染页面
    data ={
        "news_info":news.to_dict() if news else "",
        "user_info":g.user.to_dict() if g.user else "",
        "news_list":click_news_list,
        "is_collected":is_collected,
        "comments":comments_list,
        "is_followed":is_followed

    }
    return render_template("news/detail.html",data=data)
